﻿namespace MsForgeReboot.Services {
    #region Usings

    using System.Data.Entity;
    using System.Linq;

    using MsForgeReboot.Framework.Entities;
    using MsForgeReboot.Framework.Interfaces;

    using PagedList;

    #endregion

    public class UserGroupService : IUserGroupService {
        /// <summary>
        ///     The database context
        /// </summary>
        private readonly MsForgeContext context;

        /// <summary>
        ///     Initializes a new instance of the <see cref="UserGroupService" /> class.
        /// </summary>
        /// <param name="context">The context.</param>
        public UserGroupService(MsForgeContext context) {
            this.context = context;
        }

        /// <summary>
        ///     Gets the list of user groups.
        /// </summary>
        /// <param name="page">The page.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <returns></returns>
        public IPagedList<UserGroup> GetList(int page, int pageSize) {
            return this.context.UserGroups.OrderBy(x => x.Title).Select(x => x).Include(u => u.Author).ToPagedList(page, pageSize);
        }

        /// <summary>
        ///     Saves the specified model.
        /// </summary>
        /// <param name="model">The model.</param>
        public void Save(UserGroup model) {

            model.Author = this.context.Users.Single(u => u.Id == model.Author.Id);

            if (model.Id == 0) {
                this.context.UserGroups.Add(model);
            }
            else {
                this.Update(model);
            }

            this.context.SaveChanges();
        }

        /// <summary>
        ///     Gets the <see cref="UserGroup" /> by URL pointer.
        /// </summary>
        /// <param name="urlPointer">The URL pointer.</param>
        /// <returns></returns>
        public UserGroup GetByUrlPointer(string urlPointer) {
            return this.context.UserGroups.Single(item => item.UrlPointer == urlPointer);
        }

        /// <summary>
        /// Deletes the User Group specified URL pointer.
        /// </summary>
        /// <param name="urlPointer">The URL pointer.</param>
        public void Delete(string urlPointer) {
            this.context.UserGroups.Remove(this.context.UserGroups.Single(item => item.UrlPointer == urlPointer));
            this.context.SaveChanges();
        }

        /// <summary>
        ///     Updates the specified user group.
        /// </summary>
        /// <param name="userGroup">The user group.</param>
        private void Update(UserGroup userGroup)
        {
            var existingUserGroup = this.context.UserGroups.Single(ug => ug.Id == userGroup.Id);

            existingUserGroup.Author = userGroup.Author;
            existingUserGroup.Body = userGroup.Body;
            existingUserGroup.Published = userGroup.Published;
            existingUserGroup.Title = userGroup.Title;
            existingUserGroup.UrlPointer = userGroup.UrlPointer;
        }
    }
}
